package org.darcy.service.work.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.darcy.dao.WorkRoleUserMapper;
import org.darcy.entity.WorkRoleUser;
import org.darcy.entity.org.Department;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.service.org.OrgDepartmentService;
import org.darcy.service.work.WorkRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * WorkRoleUser服务实现
 */
@Service
@Transactional
public class WorkRoleUserServiceImpl extends GenericServiceImpl<WorkRoleUser> implements WorkRoleUserService {

	@Autowired
	private WorkRoleUserMapper mapper;

	@Autowired
	private OrgDepartmentService OrgDepartmentService;

	@Override
	public List<WorkRoleUser> getList() {
		List<WorkRoleUser> data = mapper.getList();
		return data;
	}

	@Override
	public List<WorkRoleUser> getManagerList(Integer roleId, Integer deptId) {

		List<WorkRoleUser> list = this.getList();
		Map<Integer, List<WorkRoleUser>> groupByRole = list.stream()
				.collect(Collectors.groupingBy(WorkRoleUser::getRoleId));
		List<WorkRoleUser> result = groupByRole.get(roleId);
		if (roleId.equals(1)) {
			if (deptId != null) {
				result = result.stream().filter(a -> deptId.equals(a.getDeptId())).collect(Collectors.toList());
			} else {
				// 部门id为空，取综合办公室
				LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
				departmentLambdaQueryWrapper.eq(Department::getName, "综合办公室");
				Department dept = OrgDepartmentService.getOne(departmentLambdaQueryWrapper);
				result = result.stream().filter(a -> dept.getId().equals(a.getDeptId())).collect(Collectors.toList());
			}
		}
		return result;

	}

}
